package dianmobile.eistarandroid.ui.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.network.request.RequestFactory;
import dianmobile.eistarandroid.utils.DataUtils;

/**
 * Created by Administrator on 2015/7/22.
 */
public class FriendsAdapter extends BaseAdapter {

    private List<String> list;
    private LayoutInflater inflater;

    public FriendsAdapter(Context context, List<String> data) {
        this.inflater = LayoutInflater.from(context);
        this.list = data;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listitem_friends, null);
            viewHolder = new ViewHolder();
            viewHolder.friendsHeader = (CircleImageView) convertView.findViewById(R.id.iv_friends_header);
            viewHolder.friendsName = (TextView) convertView.findViewById(R.id.tv_friends_name);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        String url = DataUtils.getUserHeadUrlFromName(list.get(position));
        RequestFactory.makeNetworkImage(viewHolder.friendsHeader, url, R.drawable.default_face, 0);
        viewHolder.friendsName.setText(list.get(position));

        return convertView;
    }

    class ViewHolder {
        TextView friendsName;
        CircleImageView friendsHeader;
    }
}
